package com.chookss.tiku

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialog
import com.chookss.R
import com.chookss.tiku.entity.Subject
import com.chookss.tools.Utils
import kotlinx.android.synthetic.main.dialog_datika.*
import java.util.*

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2020/12/24 9:01
 * Note: 答题卡对话框
 */

class DatikaExamRecordDialog(context: Context) : AppCompatDialog(context) {
    private var selectedList: ArrayList<String> = ArrayList()
    private var subjectss: ArrayList<Subject> = ArrayList()
    private var subjects2: ArrayList<Subject> = ArrayList()
    private var subjects3: ArrayList<Subject> = ArrayList()
    private var subjects4: ArrayList<Subject> = ArrayList()
    private var gridAdapter2: MyAdapter? = null
    private var gridAdapter3: MyAdapter? = null
    private var gridAdapter4: MyAdapter? = null
    private var listener2: DialogInterface.OnClickListener? = null
    private var listener3: DialogInterface.OnClickListener? = null
    private var listener4: DialogInterface.OnClickListener? = null
    private var width:Int=0
    var cnt1001 = 0
    var cnt1002 = 0
    var cnt1003 = 0
    var cnt1001Total = 0
    var cnt1002Total = 0
    var cnt1003Total = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view: View  = View.inflate(context, R.layout.dialog_datika, null)
        setContentView(view)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        window?.setGravity(Gravity.BOTTOM)
        window?.setWindowAnimations(R.style.DialogBottomAnim)
        initView()
    }

    @SuppressLint("ResourceType")
    fun initView() {
        ivError.visibility=View.VISIBLE
        tvError.visibility=View.VISIBLE
        ivSure.visibility=View.VISIBLE
        tvSure.visibility=View.VISIBLE
        gridView2.setSelector(ColorDrawable(Color.TRANSPARENT))
        gridView3.setSelector(ColorDrawable(Color.TRANSPARENT))
        gridView4.setSelector(ColorDrawable(Color.TRANSPARENT))
        width=(Utils.getScreenWidthPixel(context)- Utils.dip2px(context, 288F))/5
        println(width)
        gridView2.horizontalSpacing=width
        gridView3.horizontalSpacing=width
        gridView4.horizontalSpacing=width
        gridView2?.setOnItemClickListener { _, _, position, _ ->
            dismiss()
            if (listener2 != null) {
                listener2!!.onClick(this, getPosition(subjects2.get(position).subjectCode))
            }
        }
        gridView3?.setOnItemClickListener { _, _, position, _ ->
            dismiss()
            if (listener2 != null) {
                listener2!!.onClick(this, getPosition(subjects3.get(position).subjectCode))
            }
        }
        gridView4?.setOnItemClickListener { _, _, position, _ ->
            dismiss()
            if (listener2 != null) {
                listener2!!.onClick(this, getPosition(subjects4.get(position).subjectCode))
            }
        }
        close.setOnClickListener { dismiss() }
    }

    fun setItems(
        subjects: List<Subject>?, //所有题目
        listener: DialogInterface.OnClickListener?
    ): DatikaExamRecordDialog {
        if (subjects != null) {
            subjectss = subjects as ArrayList<Subject>
            subjectss.forEach {
                if(!Utils.isNull(it?.optionLvlNo)) {
                    selectedList.add(it?.optionLvlNo)
                }
                when (it?.subjectTypeCode) {
                    "1001" -> {
                        subjects2.add(it)
                        cnt1001Total++
                    }
                    "1002" -> {
                        subjects3.add(it)
                        cnt1002Total++
                    }
                    "1003" -> {
                        subjects4.add(it)
                        cnt1003Total++
                    }
                }
            }
        }
        gridAdapter2 = MyAdapter(subjects2, selectedList)
        gridAdapter3 = MyAdapter(subjects3, selectedList)
        gridAdapter4 = MyAdapter(subjects4, selectedList)
        this.listener2 = listener
        this.listener3 = listener
        this.listener4 = listener
        return this
    }

    fun getPosition(code: String): Int {
        var positionInt: Int = 0

        for (i in 0 until subjectss.size) {
            if (subjectss.get(i).subjectCode.equals(code)) {
             positionInt=i
                println(i)
            }
        }

        return positionInt
    }

    override fun onStart() {
        super.onStart()
        gridView2?.adapter = gridAdapter2
        gridView3?.adapter = gridAdapter3
        gridView4?.adapter = gridAdapter4
        if(subjects2.size>0){
            ll2.visibility=View.VISIBLE
        }else{
            ll2.visibility=View.GONE
        }
        if(subjects3.size>0){
            ll3.visibility=View.VISIBLE
        }else{
            ll3.visibility=View.GONE
        }
        if(subjects4.size>0){
            ll4.visibility=View.VISIBLE
        }else{
            ll4.visibility=View.GONE
        }
        //1001 选择题
        //1002 多选题
        //1003 判断题
        cnt1001 = 0
        cnt1002 = 0
        cnt1003 = 0
        gridAdapter2?.subjects?.forEach {
            when (it?.subjectTypeCode) {
                "1001" -> {
                    if (selectedList.contains(it?.optionLvlNo)) {
                        cnt1001++
                    }
                }
            }
        }
        gridAdapter3?.subjects?.forEach {
            when (it?.subjectTypeCode) {
                "1002" -> {
                    if (selectedList.contains(it?.optionLvlNo)) {
                        cnt1002++
                    }
                }
            }
        }
        gridAdapter4?.subjects?.forEach {
            when (it?.subjectTypeCode) {
                "1003" -> {
                    if (selectedList.contains(it?.optionLvlNo)) {
                        cnt1003++
                    }
                }
            }
        }
        var count = subjectss.size
        tv2.text = "单选题($cnt1001/$cnt1001Total)"
        tv3.text = "多选题($cnt1002/$cnt1002Total)"
        tv4.text = "判断题($cnt1003/$cnt1003Total)"


    }

    class MyAdapter(
        var subjects: List<Subject>?,
        var selectedList: ArrayList<String>
    ) : BaseAdapter() {
        override fun getCount() = subjects?.size ?: 0

        override fun getItem(position: Int) = (position + 1).toString()

        override fun getItemId(position: Int) = position.toLong()

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var view = convertView
            if (view == null) {
                view = View.inflate(parent?.context, R.layout.item_datika, null)
            }
            var tv = view!!.findViewById<TextView>(R.id.tv)
            if (selectedList.contains(subjects?.get(position)?.optionLvlNo)) {
                if("0".equals(subjects?.get(position)?.isRight)){
                    tv.isSelected = true
                    tv.background=parent?.context?.resources?.getDrawable(R.drawable.shape_circle_e43d1c)
                }else{
                    tv.background=parent?.context?.resources?.getDrawable(R.drawable.shape_circle_44d3b3)
                    tv.isSelected = true
                }
            } else {
                tv.isSelected = false
                tv.background=parent?.context?.resources?.getDrawable(R.drawable.shape_circle_white)
            }
            tv.text = getItem(position)
            return view!!
        }


    }
}